const { translateFilters } = require('../../display/filters');
const { translateValue, translateProjectile } = require('../../display/general');
const { ignoreIt } = require("../../util/ignore");
const { getModelType, diagonalLength } = require("../../util/utils");
const { parseFilters } = require("../filter/parse");
const { parseProjectileBehavior } = require("./behavior");

function parseProjectile(p) {
    let result = {
        name: translateProjectile(p.id),
        condition: null, // for sub projectiles
        pierce: p.pierce >= 9999999 ? Infinity : p.pierce,
        maxPierce: p.maxPierce > 0 && p.maxPierce < 9999999 ? p.maxPierce : null,
        piercePenalty: [],
        damage: null,
        maxDamage: null,
        damageType: null,
        damageMods: [],
        emission: {},
        traveling: {},
        radius: p.radius >= diagonalLength ? '∞' : p.radius,
        statuses: [],
        subProjectiles: [],
        behaviors: [],
        meta: {},
        filters: parseFilters(p.filters),
        notes: [
            p.ignoreBlockers ? translateValue('ignoreObstacles', 'ignore obstacles') : null,
        ]
    }
    result.pierce = result.maxPierce && result.maxPierce < result.pierce ? result.maxPierce : result.pierce
    p.behaviors.forEach(b => {
        const type = getModelType(b)
        if (ignoreIt(type)) return
        parseProjectileBehavior(b, result, type)
    });
    result.filters = translateFilters(result.filters)
    if (result.damage === null) {
        result.maxDamage = null
        result.damageMods = null
        result.damageType = null
    }
    result.pierce = result.pierce === Infinity ? '∞' : result.pierce
    return result
}

module.exports = {
    parseProjectile
}